import requests
import json
import uuid

# 1. 定义请求URL
url = "https://aimpapi.midea.com/t-aigc/labconditions/get_data_reason"

# 2. 生成唯一请求ID
request_id = str(uuid.uuid4())
print(request_id)
# 3. 构造请求数据（使用文档中的示例数据）
payload = {
    "request_id": request_id, # 唯一请求ID
    "metadata": {
      "测试项分类": "能力类",
        "mip": "cuiml10",
        "事业部": "家用空调事业部",
        "产品大类": "家用空调",
        "测试标准": "QMK-J075.0018-2020",
        "测试标准名称": "2196-EER（双焓差）",
        "委托单编号": "ST2025060032",
        "委托项编号": "52037470826"
    },

    "output_schema": {
        "室内工况干球温度": "",
        "空调样机测试运行模式": "",
        "空调样机设定风速": "",
        "室外干球温度允许偏差": "",
        "室外工况湿球温度": "",
        "室外湿球温度允许偏差": "",
        "室内干球温度允许偏差": "",
        "室外工况干球温度": "",
        "室内湿球温度允许偏差": "",
        "空调样机换气功能设定": "",
        "室内工况湿球温度": "",
        "空调样机电辅热功能设定": "",
        "空调样机设定温度": ""
    },
    "input_body": {
        "测试项名称": "2196-EER（双焓差）",
        "测试样机区域":"中国大陆",
        "测试区域电源电压(V)":"",
        "测试区域电源频率(Hz)": "",
        "样机类型": "非手板",
        "测试周期":"2"
    }
}

# 4. 设置请求头
headers = {
    "Authorization": "Bearer msk-b8623d8a816118d3836590cf4379bbd3b54595f4ef4ed98840750f6d52f16b70",
    "Content-Type": "application/json"
} 

try:
    # 5. 发送POST请求
    response = requests.post(
        url,
        headers=headers,
        data=json.dumps(payload, ensure_ascii=False).encode('utf-8')  # 处理中文字符
    )
    print(response.json())
    # 6. 检查响应状态
    if response.status_code == 200:
        # 解析成功响应
        result = response.json()
        print("✅ 请求成功!")
        print(f"请求ID: {result.get('request_id')}")
        print(f"状态码: {result.get('status_code')}")
        print("\n返回的测试工况参数:")
        print(result)
    
    # 7. 处理错误响应
    elif response.status_code == 400:
        error_data = response.json()
        print(f"❌ 请求失败 (状态码: {error_data.get('status_code')})")
        print(f"错误信息: {error_data.get('error_message')}")
    
    else:
        print(f"⚠️ 未知响应状态: {response.status_code}")
        print(f"响应内容: {response.text}")

except requests.exceptions.RequestException as e:
    print(f"🔥 网络请求异常: {str(e)}")
except json.JSONDecodeError:
    print("❌ 响应解析失败: 无效的JSON格式")
